package com.laundry.controller;

import com.laundry.dto.LoginDTO;
import com.laundry.dto.ResultDTO;
import com.laundry.entity.User;
import com.laundry.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @PostMapping("/login")
    public ResultDTO login(@RequestBody LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }
    
    @PostMapping("/register")
    public ResultDTO register(@RequestBody User user) {
        return userService.register(user);
    }
    
    @GetMapping("/info")
    public ResultDTO getUserInfo(@RequestParam Long userId) {
        return userService.getUserInfo(userId);
    }
    
    @PutMapping("/update")
    public ResultDTO updateUserInfo(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }
    
    @GetMapping("/list")
    public ResultDTO getUserList() {
        try {
            List<User> users = userService.getAllUsers();
            return ResultDTO.success(users);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取用户列表失败");
        }
    }
    
    @DeleteMapping("/delete")
    public ResultDTO deleteUser(@RequestParam Long userId) {
        try {
            userService.deleteUser(userId);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("删除用户失败");
        }
    }
} 